package cn.edu.swu.lxy.rolems.fragment

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import cn.edu.swu.lxy.rolems.R
import cn.edu.swu.lxy.rolems.service.MusicService

class MusicFragment: Fragment() {
    lateinit var playMusicBinder: MusicService.PlayMusicBinder
    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            playMusicBinder = service as MusicService.PlayMusicBinder

        }
        override fun onServiceDisconnected(name: ComponentName) {
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val view = View.inflate(activity, R.layout.fragment_music, null)

        val intent = Intent(this.context, MusicService::class.java)
        this.context?.bindService(intent, connection, Context.BIND_AUTO_CREATE) // 绑定Service
        val playMusic = view.findViewById<View>(R.id.playMusic)
        playMusic.setOnClickListener {
            playMusicBinder.playMusic()
        }

        val pauseMusic = view.findViewById<View>(R.id.pauseMusic)
        pauseMusic.setOnClickListener {
            playMusicBinder.stopMusic()
        }

        return view
    }
}